import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import Home from '@/views/index' // home
import PartakeGame from '@/views/partakeGame' // 参与游戏
import Login from '@/views/user/login'
import Resetpwd from '@/views/user/resetpwd'
import Register from '@/views/user/register'
import PrivatePage from '@/views/privatePage'
import Assets from '@/views/privatePage/assets'
import Detail from '@/views/privatePage/detail'
import Profits from '@/views/privatePage/profits'
import Invitingfriends from '@/views/share/invitingfriends'
import InvitingLink from '@/views/share/invitingLink'
import Notice from '@/views/notice'
import Noticedetails from '@/views/notice/details'

Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
      meta: {
        title: '首页'
      }
    }, {
      path: '/partakeGame',
      name: 'PartakeGame',
      component: PartakeGame,
      meta: {
        title: '参与游戏',
        current: 1
      }
    }, {
      path: '/notice',
      name: 'Notice',
      component: Notice,
      meta: {
        title: '公告列表'
      }
    }, {
      path: '/noticeDetails',
      name: 'Noticedetails',
      component: Noticedetails,
      meta: {
        title: '公告详情'
      }
    }, {
      path: '/invitingfriends',
      name: 'Invitingfriends',
      component: Invitingfriends,
      meta: {
        title: '邀请好友',
        current: 2
      }
    }, {
      path: '/invitingLink',
      name: 'InvitingLink',
      component: InvitingLink,
      meta: {
        title: '邀请好友',
        current: 2
      }
    },
    {
      path: '/',
      component: PrivatePage,
      children: [
        {
          path: '/assets',
          name: 'Assets',
          component: Assets,
          meta: {
            title: '我的资产'
          }
        },
        {
          path: '/detail',
          name: 'Detail',
          component: Detail,
          meta: {
            title: '流水明细'
          }
        },
        {
          path: '/profits',
          name: 'Profits',
          component: Profits,
          meta: {
            title: '我的分红'
          }
        }
      ]

    },
    {
      path: '/login',
      name: 'Login',
      component: Login,
      meta: {
        title: '登录'
      }
    },
    {
      path: '/resetpwd',
      name: 'Resetpwd',
      component: Resetpwd,
      meta: {
        title: '忘记密码'
      }
    },
    {
      path: '/register',
      name: 'Register',
      component: Register,
      meta: {
        title: '注册'
      }
    }
  ]
})
router.beforeEach((to, from, next) => {
  // store.commit('app_setSlideName')
  if (store.state && !to.matched.some(v => v.meta.disableLoading)) { // 路由跳转，显示菊花~ Author by Dio Zhu. on 2017.2.17
    store.commit('app_openLoading')
  }

  function setSlide(slide) {
    store.commit('app_setSlideName', slide)
  }

  if (to.query.key) {
    if (parseInt(from.query.key) > parseInt(to.query.key)) {
      setSlide('left')
    } else {
      setSlide('right')
    }
  } else {
    if (from.name) {
      setSlide('left')
    } else {
      setSlide('right')
    }
  }
  next()
})
// title设置
router.afterEach(route => {
  if (route.meta && route.meta.title) {
    document.title = route.meta.title
  } else {
    // document.title = CONFIG.TITLE
  }
  try { // 隐藏loading
    if (store && store.state) {
      setTimeout(function () {
        store.commit('app_closeLoading')
      }, 400)
    }
  } catch (e) {
    console.error('router.afterEach: ', e)
  }
})
// // 扩展路由方法
// let p = router.push
// let r = router.replace
// router.push = function (params) {
//   if (params.query) {
//     params.query.key = Date.now()
//   } else {
//     params.query = {key: Date.now()}
//   }
//   p.call(router, params)
// }
// router.replace = function (params) {
//   if (params.query) {
//     params.query.key = Date.now()
//   } else {
//     params.query = {key: Date.now()}
//   }
//   r.call(router, params)
// }
// 导出router对象
export default router
